{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95ed465c-78ae-43b9-a0a6-c00998eb19fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import load\n",
    "import numpy as np\n",
    "class Classification:   \n",
    "    def __init__(self):\n",
    "        self.knn = load('knn_classifien.joblib')\n",
    "        self.scaler = load('feature_scaler.joblib')\n",
    "        self.le = load('label_encoder.joblib')\n",
    "\n",
    "    def get_predicter_category(self, new_data):\n",
    "        \"\"\"获取分类结果\n",
    "        \"\"\"\n",
    "        # 转换为数组并保持特征顺序\n",
    "        feature_order = ['eps', 'total_revenue_ps', 'undist_profit_ps', 'gross_margin', 'fcff', 'fcfe', 'tangible_asset',\n",
    "                         'bps', 'grossprofit_margin', 'npta']\n",
    "\n",
    "        new_values = np.array([[new_data[col] for col in feature_order]])\n",
    "\n",
    "        # 标准化新数据\n",
    "        new_scaled = self.scaler.transform(new_values) \n",
    "\n",
    "        \n",
    "\n",
    "        # 预测分类\n",
    "        predicted_label = self.knn.predict(new_scaled)\n",
    "        predicted_category = self.le.inverse_transform(predicted_label)\n",
    "        return predicted_category[0]\n",
    "\n",
    "\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    ci = Classification()\n",
    "    new_data = [{\n",
    "        # 000002.SZ 2024-08-31 -0.8309 11.9673 7.0209  11586700000 -12879100000  -5263460000 179873000000  20.2568 8.1152  -0.5821\n",
    "        'eps': '-0.8309',\n",
    "        'total_revenue_ps': '11.9673',\n",
    "        'undist_profit_ps': '7.0209',\n",
    "        'gross_margin': '11586700000',\n",
    "        'fcff': '-12879100000',\n",
    "        'fcfe': '-5263460000',\n",
    "        'tangible_asset': '179873000000',\n",
    "        'bps': '20.2568',\n",
    "        'grossprofit_margin': '8.1152',\n",
    "        'upta': '-0.5821'\n",
    "    }]\n",
    "    predicted_category = ci.get_predieter_category(new_data)\n",
    "    print(predicted_category)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
